/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package adcomputer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Edgar
 */
public class AñadirDatos extends javax.swing.JPanel {
    
    public void deshabilitar()
    {
      nombrej.setEnabled(false);
      guardarDatos.setEnabled(false);
    }
    
     public void  Busqueda()
    {
            bajaj.setVisible(true);
            bajal.setVisible(true);
         try {
       BaseDeDatos bd= new BaseDeDatos();
                bd.conectar();
                String query1="Select nombre from DatosJuego";
                       ResultSet rs=bd.consultar(query1);
                while(rs.next())
                {
                    System.out.println(rs.getString(1));
                   bajaj.addItem(rs.getString(1));               
                    
                }
        } catch (SQLException ex) {
            Logger.getLogger(AñadirDatos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Creates new form AñadirDatos
     */
    public AñadirDatos() {
        try {
            initComponents();
            bajaj.setVisible(false);
            bajal.setVisible(false);
            
             BaseDeDatos bd= new BaseDeDatos();
                bd.conectar();
                String query1="Select * from Categorias";
                       ResultSet rs=bd.consultar(query1);
                while(rs.next())
                {
                    System.out.println(rs.getString(2));
                    categoriasbox.addItem(rs.getString(2));               
                    
                }
        } catch (SQLException ex) {
            Logger.getLogger(AñadirDatos.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        nombrej = new javax.swing.JTextField();
        añoj = new javax.swing.JTextField();
        empresaj = new javax.swing.JTextField();
        categoriasbox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        guardarDatos = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        existenciaj = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        precioj = new javax.swing.JTextField();
        deshabilitadoj = new javax.swing.JCheckBox();
        bajaj = new javax.swing.JComboBox();
        bajal = new javax.swing.JLabel();

        jLabel1.setText("Nombre del Juego");

        jLabel2.setText("Empresa");

        jLabel3.setText("Año de Publicacion");

        empresaj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                empresajActionPerformed(evt);
            }
        });

        categoriasbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                categoriasboxActionPerformed(evt);
            }
        });

        jLabel4.setText("Categoria");

        guardarDatos.setText("Guardar");
        guardarDatos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarDatosActionPerformed(evt);
            }
        });

        jLabel5.setText("Existencia");

        jLabel6.setText("Precio");

        deshabilitadoj.setText("Deshabilitado");
        deshabilitadoj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deshabilitadojActionPerformed(evt);
            }
        });

        bajaj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bajajActionPerformed(evt);
            }
        });

        bajal.setText("Seleccionar Juego");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(44, 44, 44)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel1)))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(bajal, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bajaj, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(nombrej, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                                .addComponent(empresaj)
                                .addComponent(añoj)
                                .addComponent(existenciaj)
                                .addComponent(categoriasbox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(guardarDatos))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(precioj, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(deshabilitadoj)))
                .addContainerGap(374, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(nombrej, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)
                        .addComponent(precioj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(empresaj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(añoj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(deshabilitadoj)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(categoriasbox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(existenciaj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bajaj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bajal))
                .addGap(26, 26, 26)
                .addComponent(guardarDatos)
                .addContainerGap(195, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void categoriasboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_categoriasboxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_categoriasboxActionPerformed

    private void guardarDatosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarDatosActionPerformed
        
         BaseDeDatos bd= new BaseDeDatos();
            bd.conectar();
            
             String nombre=nombrej.getText();
             String empresa=empresaj.getText();
             int año=Integer.parseInt(añoj.getText());
             int existencia=Integer.parseInt(existenciaj.getText());
             int precio=Integer.parseInt(precioj.getText());
             int categoria=categoriasbox.getSelectedIndex()+1;
             int deshabilitado;
             if(deshabilitadoj.isSelected())
                 deshabilitado=1;
             else
                 deshabilitado=0;
             System.out.println("cat "+categoria);
            String query="insert into  DatosJuego values (null, '"+nombre+ "','" + empresa + "'," +
                    año+"," + precio + ","+ existencia + "," + categoria + ","+ deshabilitado+")";
               System.out.println(query);
            bd.modificar(query);
            bd.desconectar();
            
            
        
    }//GEN-LAST:event_guardarDatosActionPerformed

    private void empresajActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_empresajActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_empresajActionPerformed

    private void deshabilitadojActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deshabilitadojActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_deshabilitadojActionPerformed

    private void bajajActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bajajActionPerformed
        try {
            BaseDeDatos bd= new BaseDeDatos();
            bd.conectar();
            String query = "Select * from DatosJuego where idDatosJuego=" +(bajaj.getSelectedIndex() +1);
            System.out.println(query);
            ResultSet rs=bd.consultar(query);
              while(rs.next())
                {
                    nombrej.setText(rs.getString("Nombre"));
                    empresaj.setText(rs.getString("Empresa"));
                    añoj.setText(rs.getString("Año"));
                    precioj.setText(rs.getString("precio"));
                    existenciaj.setText(rs.getString("Existencia"));
                    empresaj.setText(rs.getString("Empresa"));
                    categoriasbox.setSelectedIndex(rs.getInt("Categorias_idCategoria")-1);
                    if(rs.getInt("deshabilitado")==1)
                    {
                      deshabilitadoj.setSelected(true);
                    }
                    else
                        deshabilitadoj.setSelected(false);
                    
                }
        } catch (SQLException ex) {
            Logger.getLogger(AñadirDatos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bajajActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField añoj;
    private javax.swing.JComboBox bajaj;
    private javax.swing.JLabel bajal;
    private javax.swing.JComboBox categoriasbox;
    private javax.swing.JCheckBox deshabilitadoj;
    private javax.swing.JTextField empresaj;
    private javax.swing.JTextField existenciaj;
    private javax.swing.JButton guardarDatos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField nombrej;
    private javax.swing.JTextField precioj;
    // End of variables declaration//GEN-END:variables
}
